﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace ENG.EngStartQueueLib
{
  public abstract class Reference
  {
    public abstract string Source { get; }

    [System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Advanced)]
    private string _FileName;
    ///<summary>
    /// Sets/gets FileName value.
    ///</summary>
    public string FileName
    {
      get
      {
        return (_FileName);
      }
      set
      {
        _FileName = value;
      }
    }
    [System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Advanced)]
    private string _Path;
    ///<summary>
    /// Sets/gets FilePath value.
    ///</summary>
    public string Path
    {
      get
      {
        return (_Path);
      }
      set
      {
        _Path = value;
      }
    }
    public string FullPath
    {
      get
      {
        return
          System.IO.Path.Combine(Path, FileName);
      }
      set
      {
        this.Path = System.IO.Path.GetDirectoryName(value);
        this.FileName = System.IO.Path.GetFileName(value);
      }
    }
    [System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Advanced)]
    private string _Arguments;
    ///<summary>
    /// Sets/gets Arguments value.
    ///</summary>
    public string Arguments
    {
      get
      {
        return (_Arguments);
      }
      set
      {
        _Arguments = value;
      }
    }
    [System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Advanced)]
    private string _WorkingDirectory;
    ///<summary>
    /// Sets/gets WorkingDirectory value.
    ///</summary>
    public string WorkingDirectory
    {
      get
      {
        return (_WorkingDirectory);
      }
      set
      {
        _WorkingDirectory = value;
      }
    }
    [System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Advanced)]
    private System.Diagnostics.ProcessWindowStyle _WindowStyle;
    ///<summary>
    /// Sets/gets WindowStyle value.
    ///</summary>
    public System.Diagnostics.ProcessWindowStyle WindowStyle
    {
      get
      {
        return (_WindowStyle);
      }
      set
      {
        _WindowStyle = value;
      }
    }

    public static void AnalysePath(string pathWithArguments, out string path, out string file, out string arguments)
    {
      bool found = false;
      path = null;
      file = null;
      arguments = null;
      String regExp = @"^""((([^""]+)\\)?([^""]*))""(.*)";

      Match m = Regex.Match(pathWithArguments, regExp);
      if (m.Success)
      {
        if (m.Groups[3].Success)
          path = m.Groups[3].Value;
        else
          path = "";
        file = m.Groups[4].Value;
        if (m.Groups[5].Success)
          arguments = m.Groups[5].Value.Trim();
        else
          arguments = "";

        found = true;
      }

      if (!found)
      {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < pathWithArguments.Length; i++ )
        {
          sb.Append(pathWithArguments[i]);

          if (System.IO.File.Exists (sb.ToString()))
          {
            string pom = sb.ToString();
            path = System.IO.Path.GetDirectoryName (pom);
            file = System.IO.Path.GetFileName(pom);
            arguments = pathWithArguments.Substring(i+1);
            found = true;
            break;
          }
        }
      }
      
      if (!found)
      {
        int pom = pathWithArguments.IndexOf('-');
        int argIndex = -1;
        if (pom > -1)
          argIndex = pom;
        else
        {
        pom = pathWithArguments.IndexOf('/');
        if (pom > -1)
          argIndex = pom;
        }
        if (argIndex != -1)
        {
          arguments = pathWithArguments.Substring(argIndex);
          pathWithArguments = pathWithArguments.Substring(0, argIndex - 1);
          path = System.IO.Path.GetDirectoryName(pathWithArguments);
          file = System.IO.Path.GetFileName(pathWithArguments);
        }
        else
        {          
          path = pathWithArguments;
          file = "";
          arguments = "";
        }
      }
    }

    public static void AnalysePath(string pathWithArguments, ref Reference reference)
    {
      string p; string f; string a;
      AnalysePath(pathWithArguments, out p, out f, out a);
      reference.Path = p;
      reference.FileName = f;
      reference.Arguments = a;
    }
  }
}
